package com.example.springsecurity.common.resp;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.http.HttpStatus;

import java.io.Serializable;

import static com.example.springsecurity.common.constant.Constant.RES_FAILED_MSG;
import static com.example.springsecurity.common.constant.Constant.RES_SUCCESS_MSG;


/**
 * @Description：公共返回实体
 * @Author：fengcheng
 * @Date：2024/10/12 09:25
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> implements Serializable {
    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应结果
     */
    private T result;

    public static <T> ResponseResult<T> success(String msg) {
        return success(String.valueOf(HttpStatus.SC_OK), msg, null);
    }

    public static <T> ResponseResult<T> success(T data) {
        return success(String.valueOf(HttpStatus.SC_OK), RES_SUCCESS_MSG, data);
    }

    public static <T> ResponseResult<T> success(String code, String message, T data) {
        return buildResponseResult(code, message, data);
    }

    public static <T> ResponseResult<T> buildResponseResult(String errorCode, String message) {
        return buildResponseResult(errorCode, message, null);
    }

    public static <T> ResponseResult<T> error(String message) {
        return buildResponseResult(String.valueOf(HttpStatus.SC_BAD_REQUEST), message, null);
    }

    public static <T> ResponseResult<T> error(String code, String message) {
        return buildResponseResult(code, message, null);
    }

    public static <T> ResponseResult<T> error(String code, String message, T data) {
        return buildResponseResult(code, message, data);
    }

    public static <T> ResponseResult<T> buildResponseResult(String code, String message, T data) {
        ResponseResult<T> ResponseResult = new ResponseResult<>();
        ResponseResult.setCode(code);
        ResponseResult.setMsg(message);
        ResponseResult.setResult(data);
        return ResponseResult;
    }

    /**
     * 未登录返回结果
     */
    public static <T> ResponseResult<T> unauthorized(T result) {
        return new ResponseResult<>(String.valueOf(HttpStatus.SC_BAD_REQUEST), RES_FAILED_MSG, result);
    }

    /**
     * 未授权返回结果
     */
    public static <T> ResponseResult<T> forbidden(T result) {
        return new ResponseResult<>(String.valueOf(HttpStatus.SC_BAD_REQUEST), RES_FAILED_MSG, result);
    }
}
